package com.orm.analysis.dao.impl;

import com.orm.analysis.dao.GenericJPADAO;
import com.orm.analysis.model.Address;
import com.orm.analysis.model.ContactInfo;
import com.orm.analysis.model.Customer;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: saman
 * Date: 10/10/13
 * Time: 12:00 PM
 * To change this template use File | Settings | File Templates.
 */
public class ContactInfoDAO extends GenericJPADAO<ContactInfo, Integer> {
    /**
        * List all the authors order by lastName,firstName ascendent
        *
        * @param page the page number
        * @param size the page size
        * @return a list of authors
        */
       public List<ContactInfo> list(int page, int size) {

           Query query = this.em.createQuery("select a from ContactInfo a");
           if(page>0){
               query.setFirstResult((page - 1) * size);
               query.setMaxResults(size);
           }
           return query.getResultList();
       }


       public ContactInfo getById(Integer id) {
               return this.em.find(ContactInfo.class,id);
       }

         @Transactional
         @Override
         public void save(ContactInfo entity) {

              getEntityManager().persist(entity);


         }

    @Transactional
        @Override
            public void delete(Object id) {
                getEntityManager().remove(getEntityManager().find(ContactInfo.class, id));

             }

}
